# coding: utf-8
import web
from models.forms import *
from models.auth import Auth
from configs import *

__all__ = ['welcome', 'login', 'logout', 'signup', 'test']

class test:
    def GET(self):
        return '111'

class welcome:
    title = 'welcome'
    def GET(self):
        if 'user' in dir(web.ctx.session):
            return web.ctx.session.user
        else:
            #return render.welcome(title = self.title)
            return web.ctx.session

class login:
    title = u'User Login',
    
    def GET(self):
        return render.login(
            title = self.title,
            form = form_login,
        )
    
    def POST(self):
        post = web.input()
        
        if not form_login.validates():
            return render.login(
                title = self.title,
                form = form_login,
            )
        else:
            if Auth().login(post.username, post.password):
                web.seeother('/')
            else:
                return post
    
class signup:
    def GET(self):
        return render.signup(
            title = u'Signup',
            form = form_signup,
        )

    def POST(self):
        if not form_signup.validates():
            return render.signup(
                title = u'Signup',
                form = form_signup,
            )
        else:
            post = web.input()
            if Auth().create_user(post.email, post.password, post.username):
                web.seeother('/')
            else:
                return 'create user error'

class logout:
    def GET(self):
        try:
            if web.ctx.session.user != None:
                web.ctx.session.kill()
            
            return 'log out'
        except NameError:
            return 'failed'
    
    
class about:
    title = u'about me'
    
    def GET(self):
        pass

